package io.github.hadyang.leetcode.offer;

import java.util.Arrays;
import org.junit.Test;

/** @author haoyang.shi */
public class SumOfNDice {

  @Test
  public void test() {
    SumOfNDice(2);
    SumOfNDice(3);
  }

  public void SumOfNDice(int n) {
    if (n < 1) {
      return;
    }

    int[][] nums = new int[2][n * 6 + 1];

    int flag = 0;

    // 初始化第一个骰子各总和出现的次数
    int maxLen = nums[0].length;
    for (int i = 1; i < maxLen; i++) {
      nums[flag][i] = 1;
    }

    for (int i = 2; i <= n; i++) {
      int newFlag = flag ^ 0x01;
      Arrays.fill(nums[newFlag], 0);

      for (int j = i; j < maxLen; j++) {
        int sum = 0;

        for (int k = 1; k <= 6 && (j - k >= 0); k++) {
          sum += nums[flag][j - k];
        }

        nums[newFlag][j] = sum;
      }
      flag = newFlag;
    }

    // debug out
    System.out.println(Arrays.toString(nums[flag]));

    int sum = 0;
    for (int i : nums[flag]) {
      sum += i;
    }

    for (int i = 0; i < nums[flag].length; i++) {
      System.out.println(i + ":" + nums[flag][i] * 1.0 / sum);
    }
  }
}
